const {
	reqGet,
	reqPost,
} = require('@utils/request')
const {
	$U_UserInfo,
	$U_LocalUserInfo,
	$U_SetItem,
	$U_GetItem,
	$U_RemoveItem,
} = require('@utils/storage')

module.exports = {

	updateMethod(type) {
		let {
			pwdComp,
			emailComp,
			nicknameComp,
		} = this
		let userInfo = $U_UserInfo()
		let oldPassword = userInfo.orgPassword
		let newPassword = pwdComp.passwordbox('getValue')
		let nickname = nicknameComp.textbox('getValue')
		let email = emailComp.textbox('getValue')
		if (type == 'pwd' && oldPassword == newPassword)
			return false
		if (type != 'pwd' && oldPassword != newPassword)
			return false
		let params = {
			id: userInfo.id,
			account: userInfo.account,
			type,
			password: oldPassword,
		}
		if (type == 'pwd') {
			params.newpassword = newPassword
		} else if (type == 'nickname') {
			params.nickname = nickname
		} else if (type == 'email') {
			params.email = email
		}
		reqPost('/ult/user/update', params).then(result => {
			let msg = ''
			if (result && result.id && result.account) {
				result.orgPassword = newPassword
				pwdComp.passwordbox('setValue', newPassword)
				nicknameComp.textbox('setValue', nickname)
				emailComp.textbox('setValue', email)
				$U_UserInfo(result)
				msg = '修改成功！'
			}
			msg = msg || JSON.stringify(result, null, 5)
			$.messager.alert({
				title: '提示',
				msg,
			})
		})
	},

	recordPassword(checked) {
		if (!checked) {
			$U_RemoveItem('user')
			return false
		}
		let cacheUser = $U_UserInfo()
		$U_LocalUserInfo(cacheUser)
	},
}